/*
 * Name        : CouponHttpBaseTask.java
 * Classes     : CouponHttpBaseTask
 * Version     : 1.0
 * Date        : 13-10-23
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http.engine;



import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.http.BaseRequest;
import com.xgkj.diyiketang.http.BaseResponse;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.HttpResponseCode;

/**
 * Class Name  : CouponHttpBaseTask
 * Author      : Tu Yuanlin
 * Date        : 13-10-23
 * Description : Get balance task
 */
public abstract class CouponHttpBaseTask extends BaseTask{
    /**
     * Constructor of BaseTask, init members and assign task id.
     *
     * @param type task type
     */
    protected CouponHttpBaseTask(int type) {
        super(type);
    }

    protected CouponHttpBaseTask(){
        this(1);
    }
    
    @Override
    public void handleResponse(BaseRequest request, BaseResponse response) {
        super.handleResponse(request, response);
        if (response instanceof CouponHttpResponse) {
            CouponHttpResponse cResponse = (CouponHttpResponse) response;
            if( cResponse.getResult() == HttpResponseCode.HTTP_RESPONSE_FAIL_TIMEOUT ){
                notifyTaskError(TaskResultCode.NETWORK_CONNECT_ERROR, null);
            }else if(cResponse.getResult() == HttpResponseCode.HTTP_RESPONSE_FAIL_CONNECT) {
                notifyTaskError(TaskResultCode.NETWORK_CONNECT_ERROR, null);
            }else if(cResponse.getResult() == HttpResponseCode.HTTP_RESPONSE_FAIL_STATUS_CODE){
            	notifyTaskError(TaskResultCode.SERVER_CONNECT_ERROR, null);
            } else if (cResponse.getResult() != HttpResponseCode.HTTP_RESPONSE_SUCCEED) {
                notifyTaskError(TaskResultCode.GENERAL_ERROR, null);
            } else if(!(cResponse.getCouponResultCode().equals(ProtocolConst.RSPCD_VALUE_SUCCESS))  && cResponse.getCouponResultCode().equals(ProtocolConst.RSPCD_VALUE_NEED_LOGIN)){
                notifyTaskError(TaskResultCode.NEED_LOGIN, ((CouponHttpResponse) response).getCouponErrorInfo());
            }else if (cResponse.getCouponResultCode().equals(ProtocolConst.RSPCD_VALUE_SUCCESS)) {
                processResponseData(cResponse);
            } else{
                int responseCode = TaskResultCode.CUSTOME_RESULT_CODE;
                try {
                    responseCode = Integer.parseInt(cResponse.getCouponResultCode());
                }catch (Exception e){

                }

            	notifyTaskError(responseCode, ((CouponHttpResponse) response).getCouponErrorInfo());
            }
        }
    }

    protected abstract void processResponseData(CouponHttpResponse response);
    
    protected JSONObject getBaseReq(){
    	JSONObject json = new JSONObject();
//    	json.put("userid", YunboxApp.getInstance().getUserId());
//    	json.put("deviceid", YunboxApp.getInstance().getDeviceId());
//    	json.put("sessionid", YunboxApp.getInstance().getSessionId());
    	return json;
    }
}
